import { app } from "../../scripts/app.js";

// 简单的自定义 Widget 类
class SimpleCustomWidget {
    constructor() {
        this.value = null;
        this.element = null;
        this.node = null;
        this.name = null;
        this.type = "simple_custom";
        console.log(`[SimpleCustomWidget] 创建简单自定义小部件`);
    }

    init(node, name, inputData) {
        console.log(`[SimpleCustomWidget] 初始化:`, { 
            node: node?.type, 
            name, 
            inputData 
        });
        
        this.node = node;
        this.name = name;
        this.value = inputData?.default || "默认值";
        this.element = this.createElement();
        
        return this;
    }

    createElement() {
        console.log(`[SimpleCustomWidget] 创建元素`);
        
        const div = document.createElement("div");
        div.style.width = "100%";
        div.style.padding = "8px";
        div.style.backgroundColor = "var(--comfy-input-bg)";
        div.style.border = "2px solid var(--border-color)";
        div.style.borderRadius = "8px";
        div.style.margin = "4px 0";
        div.style.fontSize = "12px";
        div.style.color = "var(--input-text)";
        div.textContent = `自定义 Widget: ${this.value}`;
        
        return div;
    }

    getValue() {
        return this.value;
    }

    setValue(value) {
        this.value = value;
        if (this.element) {
            this.element.textContent = `自定义 Widget: ${value}`;
        }
    }

    serialize() {
        return this.value;
    }
}

// 注册扩展
app.registerExtension({
    name: "simple.custom.widget.test",
    
    async nodeCreated(node) {
        console.log(`[SimpleTest] 节点创建: ${node.type}`);
        
        // 检查节点是否有 addCustomWidget 方法
        if (typeof node.addCustomWidget === 'function') {
            console.log(`[SimpleTest] 节点支持 addCustomWidget 方法`);
            
            try {
                // 创建自定义 Widget 实例
                const customWidget = new SimpleCustomWidget();
                customWidget.init(node, "simple_test", {
                    default: "测试值",
                    description: "简单测试"
                });
                
                // 使用 addCustomWidget 方法
                const result = node.addCustomWidget(customWidget);
                
                console.log(`[SimpleTest] addCustomWidget 调用结果:`, result);
                
                // 检查 widgets 数组
                if (node.widgets) {
                    console.log(`[SimpleTest] 节点 widgets 数组:`, node.widgets);
                    console.log(`[SimpleTest] widgets 数量:`, node.widgets.length);
                }
                
            } catch (error) {
                console.error(`[SimpleTest] addCustomWidget 调用失败:`, error);
            }
        } else {
            console.log(`[SimpleTest] 节点不支持 addCustomWidget 方法`);
            
            // 尝试手动添加
            try {
                const customWidget = new SimpleCustomWidget();
                customWidget.init(node, "manual_test", {
                    default: "手动测试值"
                });
                
                if (!node.widgets) {
                    node.widgets = [];
                }
                node.widgets.push(customWidget);
                
                console.log(`[SimpleTest] 手动添加自定义 Widget 成功`);
                console.log(`[SimpleTest] widgets 数量:`, node.widgets.length);
                
            } catch (error) {
                console.error(`[SimpleTest] 手动添加失败:`, error);
            }
        }
    }
}); 